package com.zyd.service.login.impl;

import com.zyd.dao.UserDao;
import com.zyd.pojo.User;
import com.zyd.service.login.LoginService;
import com.zyd.util.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findByMobile(String mobile) {
        return userDao.findByUsername(mobile);
    }
       @Override
    public String getCode(String mobile) throws Exception {

        //生成code
		
        String code = (int) ((Math.random()*9+1)*100000) + "";
        long time = new Date().getTime();
        String s = code + "&" + time;
        System.out.println(s);
        System.out.println(mobile);
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("code", code);
        HttpClient client = new HttpClient("http://localhost:8080/message/send", map);
        client.post();
        Integer httpcode = client.getStatusCode();
        if (httpcode != 200) {
            throw new RuntimeException("发送失败");
        }
        return s;
    }

    @Override
    public User login(String username, String password) {
        return (User) userDao.findByUsernameAndPassword(username, password);
    }


}
